<?php


namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Category;

class CategoryController extends Controller
{
    public function actionList(){
        $query = Category::find();
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '20']);
        $cate = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();


        return $this->render("list",['cates'=>$cate,'p'=>$pages]);
    }

    public function actionAdd(){
        $cate = Category::find()->where(['flag'=>1,'p_id'=>0])->asArray()->all();

        return $this->render("add",['cates'=>$cate]);
    }

    public function actionAddCate(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $cate = new Category();
            $cate->name=$request->post("name");
            $cate->p_id=$request->post("cate");
            $cate->p_name=$request->post("cateName");
            $cate->tpl=$request->post("tpl");
            $return = [];
            if($cate->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($cate->errors);
            }
            return json_encode($return);
        }
    }

    public function actionEdit(){
        $request = Yii::$app->request;
        $id=$request->get("id");
        $cates = Category::find()->where(['flag'=>1,'p_id'=>0])->asArray()->all();

        $cate = Category::findOne($id);
        return $this->render("edit",['cates'=>$cates,"cate"=>$cate]);
    }

    public function actionEditCate(){
        $request= Yii::$app->request;
        if($request->isAjax){
            $id=$request->post("id");
            $cate = Category::findOne($id);
            $cate->name=$request->post("name");
            $cate->p_id=$request->post("cate");
            $cate->p_name=$request->post("cateName");
            $cate->tpl=$request->post("tpl");
            $return = [];
            if($cate->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($cate->errors);
            }
            return json_encode($return);
        }
    }

    public function actionDel(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $id=$request->post("id");
            $cate = Category::findOne($id);
            $return = [];
            if($cate->delete()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($cate->errors);
            }
            return json_encode($return);
        }
    }

    public function actionDisable(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $id=$request->post("id");
            $cate = Category::findOne($id);
            $return = [];
            $cate->flag=0;
            if($cate->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($cate->errors);
            }
            return json_encode($return);
        }
    }

    public function actionEnable(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $id=$request->post("id");
            $cate = Category::findOne($id);
            $return = [];
            $cate->flag=1;
            if($cate->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($cate->errors);
            }
            return json_encode($return);
        }
    }
}